﻿using System;
using System.Collections.Generic;
using NReader.Model.RAS;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XnReader.WinRT.Helper;

// “项目页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234233 上提供

namespace XnReader.WinRT
{
    /// <summary>
    /// 显示项预览集合的页。在“拆分布局应用程序”中，此页
    /// 用于显示及选择可用组之一。
    /// </summary>
    public sealed partial class BookList
    {
        public BookList()
        {
            InitializeComponent();

            var toolBar = new ToolBarInfo
            {
                VisibleAddStore = Visibility.Collapsed,
                VisibleBookReader = Visibility.Collapsed,
                VisibleCommend = Visibility.Collapsed,
                VisibleContactUs = Visibility.Collapsed,
                VisibleDelStore = Visibility.Collapsed,
                VisibleLogin = Visibility.Collapsed,
                VisibleRights = Visibility.Collapsed,
                VisibleSearch = Visibility.Visible,
                VisibleSetting = Visibility.Collapsed
            };
            if (tlBottom != null) tlBottom.DataContext = toolBar;
        }

        private ArticleListIncrementalLoadingClass<ArticleInfo> _articleList;

        private async void DataBinding(int sortid)
        {
            try
            {
                var count = await NReader.Service.ArticleService.Instance().GetArticleListCountBySort(sortid);
                _articleList = new ArticleListIncrementalLoadingClass<ArticleInfo>((uint) count, (uint) sortid);
                cvsArticleList.Source = _articleList;
                pbLoading.IsActive = false;
            }
            catch (Exception)
            {
                var md = new MessageDialog("书籍列表加载错误.请检查您的网络链接是否正确");
                md.ShowAsync();
                //throw;
            }
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (await GlobalUtils.CheckConnectionState())
            {
                var sortid = 1; //默认为1
                if (!string.IsNullOrEmpty(navigationParameter.ToString()))
                {
                    sortid = int.Parse(navigationParameter.ToString());
                    txtTitle.Text = GlobalUtils.GetSortName(sortid);
                }
                DataBinding(sortid);
            }
            else
            {
                var md = new MessageDialog("书籍列表加载错误.请检查您的网络链接是否正确");
                md.ShowAsync();
            }
        }

        private void ItemGridViewItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem != null)
            {
                var articleid = ((ArticleInfo)e.ClickedItem).articleid;
                Frame.Navigate(typeof(Book), articleid);
            }
        }
    }
}
